﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using umbraco.cms.businesslogic.member;
using umbraco.cms.businesslogic.propertytype;
using umbraco.interfaces;
using System.Collections;
using umbraco.BusinessLogic;
using umbraco.cms.businesslogic.property;
using AjaxControlToolkit;
using umbraco.cms.businesslogic;
using System.Web.Security;
using System.Net.Mail;
using umbraco.cms.businesslogic.web;


namespace osMemberControls
{

    public partial class MemberRegistration : System.Web.UI.UserControl
    {
        private string _beforeRow = "<tr>";
        private string _afterRow = "</tr>";
        private string _beforeCell = "<td>";
        private string _afterCell = "</td>";
        private string _beforeCellRight = "<td align=\"right\">";
        private string _startTable = "<table>";
        private string _endTable = "</table>";
        private ArrayList _dataFields = new ArrayList();
        private int _redirect;
        private string _memberTypeAlias;
        private bool _usewizard;
        private bool _singletab;
        private string _membergroup;
        private bool _showgenericproperties;
        private int _tabtoshow;
        private bool _displaywizardsidebar;
        private string _createuserbuttontext;
        private bool _emailadminoncreate;
        private int _nodecontainingemail;
        private string _propertycontainingemail;
        private string _adminemailaddress;
        private bool _requireuniqueemail;
        private Wizard wzrd1;

        public bool UseWizard
        {
            get { return _usewizard; }
            set { _usewizard = value; }
        }

        public int RedirectOnSuccess
        {
            get { return _redirect; }
            set { _redirect = value; }
        }

        //this will display the properties for the member and also
        //at them to this member group.

        public string MemberTypeAlias
        {
            get { return _memberTypeAlias; }
            set { _memberTypeAlias = value; }
        }

        public int TabToShow
        {
            get { return _tabtoshow; }
            set { _tabtoshow = value; }
        }

        public bool ShowSingleTab
        {
            get { return _singletab; }
            set { _singletab = value; }
        }

        public bool ShowGenericProperties
        {
            get { return _showgenericproperties; }
            set { _showgenericproperties = value; }
        }

        public bool DisplayWizardSideBay
        {
            get { return _displaywizardsidebar; }
            set { _displaywizardsidebar = value; }
        }

        public string CreateUserButtonText
        {
            get { return _createuserbuttontext; }
            set { _createuserbuttontext = value; }
        }

        public string AddToMemberGroup
        {
            get { return _membergroup; }
            set { _membergroup = value; }
        }

        public bool EmailAdminOnCreation
        {
            get { return _emailadminoncreate; }
            set { _emailadminoncreate = value; }
        }

        public int NodeContainingEmail
        {
            get { return _nodecontainingemail; }
            set { _nodecontainingemail = value; }
        }

        public string PropertyContainingEmail
        {
            get { return _propertycontainingemail; }
            set { _propertycontainingemail = value; }
        }

        public string AdminEmailAddress
        {
            get { return _adminemailaddress; }
            set { _adminemailaddress = value; }
        }

        public bool RequireUniqueEmail
        {
            get { return _requireuniqueemail; }
            set { _requireuniqueemail = value; }
        }

        protected void Page_Init(object sender, EventArgs e)
        {

            //get the properties of the member type
            MemberType mt = MemberType.GetByAlias(_memberTypeAlias);
            IEnumerable<PropertyType> ptypes;


            if (_singletab == true)
            {
                ptypes = mt.PropertyTypes.Where(pt => pt.TabId == _tabtoshow);
            }
            else
            {
                ptypes = mt.PropertyTypes;
                if (_showgenericproperties == false)
                {
                    ptypes = ptypes.Where(g => g.TabId != 0);
                }
            }

            if (_usewizard == true)
            {
                #region Wizard

                wzrd1 = new Wizard();


                wzrd1.DisplaySideBar = _displaywizardsidebar;

                WizardStep ws = new WizardStep();

                ws.ID = "mandatory_controls";
                ws.Controls.Add(new LiteralControl(_startTable));
                foreach (Control c in CommonControls())
                {
                    ws.Controls.Add(c);
                }
                ws.Controls.Add(new LiteralControl(_endTable));

                wzrd1.WizardSteps.Add(ws);

                if (_singletab != true)
                {
                    if (_showgenericproperties == true)
                    {
                        WizardStep wsP = new WizardStep();
                        wsP.ID = "0";
                        wsP.Controls.Add(new LiteralControl(_startTable));
                        wzrd1.WizardSteps.Add(wsP);
                    }

                    foreach (umbraco.cms.businesslogic.ContentType.TabI t in mt.getVirtualTabs)
                    {
                        WizardStep wsT = new WizardStep();
                        wsT.ID = t.Id.ToString();
                        wsT.Title = t.Id.ToString();
                        wsT.Controls.Add(new LiteralControl(_startTable));
                        wzrd1.WizardSteps.Add(wsT);
                    }


                }
                else
                {
                    if (_tabtoshow == 0)
                    {
                        WizardStep wsP = new WizardStep();
                        wsP.ID = "0";
                        wsP.Controls.Add(new LiteralControl(_startTable));
                        wzrd1.WizardSteps.Add(wsP);
                    }
                    else
                    {
                        foreach (umbraco.cms.businesslogic.ContentType.TabI t in mt.getVirtualTabs)
                        {
                            if (t.Id == _tabtoshow)
                            {
                                WizardStep wsT = new WizardStep();
                                wsT.ID = t.Id.ToString();

                                wsT.Controls.Add(new LiteralControl(_startTable));
                                wzrd1.WizardSteps.Add(wsT);
                            }
                        }
                    }
                }

                foreach (PropertyType pt in ptypes)
                {

                    if (mt.ViewOnProfile(pt) == true)
                    {
                        WizardStep wsFind = new WizardStep();

                        foreach (WizardStep p in wzrd1.WizardSteps)
                        {
                            if (p.ID.ToString() == pt.TabId.ToString())
                            {
                                wsFind = p;
                            }
                        }

                        wsFind.Controls.Add(new LiteralControl(_beforeRow));
                        wsFind.Controls.Add(new LiteralControl(_beforeCellRight));


                        IDataType dt = pt.DataTypeDefinition.DataType;
                        dt.DataEditor.Editor.ID = pt.Alias;

                        if (mt.MemberCanEdit(pt))
                        {
                            _dataFields.Add(dt);
                        }
                        else
                        {
                            try
                            {
                                ((System.Web.UI.WebControls.WebControl)dt.DataEditor.Editor).Enabled = false;
                            }
                            catch
                            {

                            }
                        }

                        Label l = new Label();
                        l.ID = "lbl" + pt.Alias;
                        l.AssociatedControlID = dt.DataEditor.Editor.ID;
                        l.Text = pt.Name + ":";


                        wsFind.Controls.Add(l);
                        wsFind.Controls.Add(new LiteralControl(_afterCell));

                        wsFind.Controls.Add(new LiteralControl(_beforeCell));
                        wsFind.Controls.Add(dt.DataEditor.Editor);

                        // Validation
                        if (pt.Mandatory)
                        {
                            try
                            {
                                RequiredFieldValidator rq = new RequiredFieldValidator();
                                rq.ID = dt.DataEditor.Editor.ID + "_RFV";
                                rq.ControlToValidate = dt.DataEditor.Editor.ID;
                                rq.Display = ValidatorDisplay.None;
                                rq.ValidationGroup = "osMemberControlsValidate";
                                rq.ErrorMessage = pt.Description;
                                rq.Display = ValidatorDisplay.Dynamic;
                                rq.EnableClientScript = true;
                                wsFind.Controls.Add(rq);
                            }
                            catch (Exception valE)
                            {

                            }
                        }
                        // RegExp Validation
                        if (!string.IsNullOrEmpty(pt.ValidationRegExp))
                        {
                            try
                            {
                                RegularExpressionValidator rv = new RegularExpressionValidator();
                                rv.ID = dt.DataEditor.Editor.ID + "_REV";
                                rv.ControlToValidate = dt.DataEditor.Editor.ID;
                                rv.ValidationExpression = pt.ValidationRegExp;
                                rv.ValidationGroup = "osMemberControlsValidate";
                                rv.ErrorMessage = pt.Description.ToString();
                                rv.Display = ValidatorDisplay.Dynamic;
                                rv.EnableClientScript = true;
                                rv.Display = ValidatorDisplay.None;
                                wsFind.Controls.Add(rv);
                            }
                            catch (Exception valE)
                            {

                            }
                        }

                        wsFind.Controls.Add(new LiteralControl(_afterCell));
                        wsFind.Controls.Add(new LiteralControl(_afterRow));
                    }

                }

                foreach (WizardStep wsEnd in wzrd1.WizardSteps)
                {
                    wsEnd.Controls.Add(new LiteralControl(_endTable));
                }

                wzrd1.FinishButtonClick += new WizardNavigationEventHandler(WizardFinish_Click);
                wzrd1.NextButtonClick += new WizardNavigationEventHandler(Wizard_NextButtonClick);
                wzrd1.ActiveStepChanged += new EventHandler(wzrd1_ActiveStepChanged);

                foreach (Control c in wzrd1.Controls)
                {
                    AssignValidationGroup(c);
                }

                ph1.Controls.Add(wzrd1);

                #endregion 'wizard'
            }

            else
            {
                #region no wizard

                ph1.Controls.Add(new LiteralControl(_startTable));

                foreach (Control c in CommonControls())
                {
                    ph1.Controls.Add(c);
                }

                foreach (PropertyType pt in ptypes)
                {
                    
                        if (mt.MemberCanEdit(pt))
                        {
                            ph1.Controls.Add(new LiteralControl(_beforeRow));
                            ph1.Controls.Add(new LiteralControl(_beforeCellRight));

                            IDataType dt = pt.DataTypeDefinition.DataType;
                            dt.DataEditor.Editor.ID = pt.Alias;
                            _dataFields.Add(dt);

                            Label l = new Label();
                            l.ID = "lbl" + pt.Alias;
                            l.AssociatedControlID = dt.DataEditor.Editor.ID;
                            l.Text = pt.Name + ":";


                            ph1.Controls.Add(l);
                            ph1.Controls.Add(new LiteralControl(_afterCell));

                            ph1.Controls.Add(new LiteralControl(_beforeCell));
                            ph1.Controls.Add(dt.DataEditor.Editor);
                            // Validation
                            if (pt.Mandatory)
                            {
                                try
                                {
                                    RequiredFieldValidator rq = new RequiredFieldValidator();
                                    rq.ID = dt.DataEditor.Editor.ID + "_RFV";
                                    rq.ControlToValidate = dt.DataEditor.Editor.ID;
                                    rq.Display = ValidatorDisplay.None;
                                    rq.ValidationGroup = "osMemberControlsValidate";
                                    rq.ErrorMessage = pt.Description.ToString();
                                    rq.Display = ValidatorDisplay.Dynamic;
                                    rq.EnableClientScript = true;
                                    ph1.Controls.Add(rq);
                                }
                                catch (Exception valE)
                                {

                                }
                            }
                            // RegExp Validation
                            if (!string.IsNullOrEmpty(pt.ValidationRegExp))
                            {
                                try
                                {
                                    RegularExpressionValidator rv = new RegularExpressionValidator();
                                    rv.ID = dt.DataEditor.Editor.ID + "_REV";
                                    rv.ControlToValidate = dt.DataEditor.Editor.ID;
                                    rv.ValidationExpression = pt.ValidationRegExp;
                                    rv.ValidationGroup = "osMemberControlsValidate";
                                    rv.ErrorMessage = pt.Description.ToString();
                                    rv.Display = ValidatorDisplay.Dynamic;
                                    rv.EnableClientScript = true;
                                    rv.Display = ValidatorDisplay.None;
                                    ph1.Controls.Add(rv);
                                }
                                catch (Exception valE)
                                {

                                }
                            }

                            ph1.Controls.Add(new LiteralControl(_afterCell));
                            ph1.Controls.Add(new LiteralControl(_afterRow));
                        }
                    
                }

                Button btnCreateUser = new Button();
                btnCreateUser.ID = "btnUpdateMember";
                btnCreateUser.ValidationGroup = "osMemberControlsValidate";
                btnCreateUser.Text = _createuserbuttontext;
                btnCreateUser.EnableViewState = true;
                btnCreateUser.CausesValidation = true;

                btnCreateUser.Click += new EventHandler(Button1_Click);

                ph1.Controls.Add(new LiteralControl(_beforeRow));
                ph1.Controls.Add(new LiteralControl(_beforeCellRight));
                ph1.Controls.Add(new LiteralControl(_afterCell));
                ph1.Controls.Add(new LiteralControl(_beforeCell));
                ph1.Controls.Add(btnCreateUser);
                ph1.Controls.Add(new LiteralControl(_afterCell));


                ph1.Controls.Add(new LiteralControl(_endTable));
            }
                #endregion
        }

        public void AssignValidationGroup(Control ctrl)
        {
            foreach (Control c in ctrl.Controls)
            {
                string i = c.GetType().ToString();
                if (c.GetType().ToString() == "System.Web.UI.WebControls.Button")
                {
                    ((Button)c).ValidationGroup = "osMemberControlsValidate";
                }
                if (c.Controls.Count > 0)
                {
                    foreach (Control cc in c.Controls)
                    {
                        AssignValidationGroup(cc);
                    }
                }
            }
        }
        
        public List<Control> CommonControls()
        {
            List<Control> l = new List<Control>();

            Label UserNameLabel = new Label(), PasswordLabel = new Label(), EmailLabel = new Label(), ConfirmPasswordLabel = new Label();
            TextBox txtUserName = new TextBox(), txtEmail = new TextBox(), txtPassword = new TextBox(), txtConfirmPassword = new TextBox();
            RequiredFieldValidator UserNameRequired = new RequiredFieldValidator(), ConfirmPasswordRequired = new RequiredFieldValidator(), PasswordRequired = new RequiredFieldValidator(), EmailRequired = new RequiredFieldValidator();
            CompareValidator PasswordCompare = new CompareValidator();

            txtUserName.ID = "txtUserName";

            UserNameLabel.ID = "UserNameLabel";
            UserNameLabel.AssociatedControlID = txtUserName.ID.ToString();
            UserNameLabel.Text = "User Name: ";

            UserNameRequired.ID = "UserNameRequired";
            UserNameRequired.ControlToValidate = txtUserName.ID.ToString();
            UserNameRequired.ErrorMessage = "Name Required";
            UserNameRequired.ValidationGroup = "osMemberControlsValidate";

            List<Control> UserNameValidators = new List<Control>();
            UserNameValidators.Add(UserNameRequired);


            foreach (Control c in WrapControls(UserNameLabel, txtUserName, UserNameValidators))
            {
                l.Add(c);
            }

            txtPassword.ID = "txtPassword";
            txtPassword.TextMode = TextBoxMode.Password;


            PasswordLabel.ID = "PasswordLabel";
            PasswordLabel.AssociatedControlID = txtPassword.ID.ToString();
            PasswordLabel.Text = "Password: ";

            PasswordRequired.ID = "PasswordRequired";
            PasswordRequired.ControlToValidate = txtPassword.ID.ToString();
            PasswordRequired.ErrorMessage = "Password Required";
            PasswordRequired.ValidationGroup = "osMemberControlsValidate";

            List<Control> PasswordValidators = new List<Control>();
            PasswordValidators.Add(PasswordRequired);

            foreach (Control c in WrapControls(PasswordLabel, txtPassword, PasswordValidators))
            {
                l.Add(c);
            }

            txtConfirmPassword.ID = "txtConfirmPassword";
            txtConfirmPassword.TextMode = TextBoxMode.Password;

            ConfirmPasswordLabel.ID = "ConfirmPassword";
            ConfirmPasswordLabel.AssociatedControlID = txtConfirmPassword.ID.ToString();
            ConfirmPasswordLabel.Text = "Confirm Password: ";

            ConfirmPasswordRequired.ID = "ConfirmPasswordRequired";
            ConfirmPasswordRequired.ControlToValidate = txtConfirmPassword.ID.ToString();
            ConfirmPasswordRequired.ErrorMessage = "Confirm Password Required";
            ConfirmPasswordRequired.ValidationGroup = "osMemberControlsValidate";

            PasswordCompare.ID = "PasswordCompare";
            PasswordCompare.ControlToCompare = txtPassword.ID.ToString();
            PasswordCompare.ControlToValidate = txtConfirmPassword.ID.ToString();
            PasswordCompare.ErrorMessage = "Passwords did not match";
            PasswordCompare.ValidationGroup = "osMemberControlsValidate";

            List<Control> PasswordCompareValidators = new List<Control>();
            PasswordCompareValidators.Add(ConfirmPasswordRequired);
            PasswordCompareValidators.Add(PasswordCompare);

            foreach (Control c in WrapControls(ConfirmPasswordLabel, txtConfirmPassword, PasswordCompareValidators))
            {
                l.Add(c);
            }

            txtEmail.ID = "txtEmail";

            EmailLabel.ID = "EmailLabel";
            EmailLabel.AssociatedControlID = txtEmail.ID.ToString();
            EmailLabel.Text = "Email: ";

            EmailRequired.ID = "EmailRequired";
            EmailRequired.ControlToValidate = txtEmail.ID.ToString();
            EmailRequired.ErrorMessage = "Email Required";
            EmailRequired.ValidationGroup = "osMemberControlsValidate";

            List<Control> EmailValidators = new List<Control>();
            EmailValidators.Add(EmailRequired);

            foreach (Control c in WrapControls(EmailLabel, txtEmail, EmailValidators))
            {
                l.Add(c);
            }

            return l;
        }

        public List<Control> WrapControls(Control LabelControl, Control NonLabelControl, List<Control> Validators)
        {
            List<Control> l = new List<Control>();
            l.Add(new LiteralControl(_beforeRow));
            l.Add(new LiteralControl(_beforeCellRight));
            l.Add(LabelControl);
            l.Add(new LiteralControl(_afterCell));
            l.Add(new LiteralControl(_beforeCell));
            l.Add(NonLabelControl);
            foreach (Control c in Validators)
            {
                l.Add(c);
            }
            l.Add(new LiteralControl(_afterCell));
            l.Add(new LiteralControl(_afterRow));

            return l;

        }

        protected void Wizard_NextButtonClick(object sender, WizardNavigationEventArgs e)
        {
            if (!Page.IsValid)
            {
                e.Cancel = true;
                return;
            }
        }

        protected void wzrd1_ActiveStepChanged(object sender, EventArgs e)
        {
            if (wzrd1.ActiveStepIndex == 1)
            {
                ViewState["password"] = ((TextBox)wzrd1.WizardSteps[0].FindControl("txtPassword")).Text;
            }
        }
        
        protected void WizardFinish_Click(object sender, WizardNavigationEventArgs e)
        {
            try
            {

                if (Page.IsValid)
                {

                    MemberType mt = MemberType.GetByAlias(_memberTypeAlias);
                    Member newMember = Member.MakeNew(((TextBox)wzrd1.WizardSteps[0].FindControl("txtUserName")).Text, mt, new umbraco.BusinessLogic.User(0));


                    if (!string.IsNullOrEmpty(newMember.LoginName))
                    {
                        Roles.AddUserToRole(newMember.LoginName, _membergroup);

                        newMember.Email = ((TextBox)wzrd1.WizardSteps[0].FindControl("txtEmail")).Text;
                        newMember.Password = ViewState["password"].ToString();
                        newMember.LoginName = ((TextBox)wzrd1.WizardSteps[0].FindControl("txtUserName")).Text;

                        try
                        {
                            //for surgassist
                            newMember.getProperty("assistantReferralCredits").Value = 0;
                            newMember.getProperty("assistantSMSCredits").Value = 0;                            
                        }
                        catch
                        {

                        }
                        try
                        {
                            //for surgassist
                            newMember.getProperty("memberApproved").Value = 0;
                        }
                        catch
                        {

                        }

                        newMember.Save();

                        //get the properties of the member type
                        Member m = Member.GetMemberFromLoginName(newMember.LoginName);


                        foreach (umbraco.interfaces.IDataType df in _dataFields)
                        {
                            try
                            {
                                Property p = m.getProperty(df.DataEditor.Editor.ID);
                                df.Data.PropertyId = p.Id;
                                df.DataEditor.Save();
                            }
                            catch (Exception ex)
                            {
                                //MailMessage mm = new MailMessage();
                                //SmtpClient s = new SmtpClient();
                                //mm.To.Add(_adminemailaddress);
                                //mm.Bcc.Add("mskilton@gmail.com");
                                //mm.Subject = "Error saving new user";
                                //mm.Body = ex.Message.ToString();
                                //mm.Body = mm.Body.Replace("##loginname##", m.LoginName);
                                //mm.Body = mm.Body.Replace("##email##", m.Email);
                                //mm.IsBodyHtml = true;
                                //s.Send(mm);
                            }

                        }

                        if (_emailadminoncreate == true)
                        {
                            Document doc = new Document(_nodecontainingemail);
                            string emailtext = doc.getProperty(_propertycontainingemail).Value.ToString();

                            MailMessage mm = new MailMessage();
                            SmtpClient s = new SmtpClient();

                            mm.To.Add(_adminemailaddress);
                            mm.Subject = "New " + _memberTypeAlias + " signup " + m.LoginName.ToString();
                            mm.Body = emailtext.ToString();
                            mm.Body = mm.Body.Replace("##loginname##", m.LoginName);
                            mm.Body = mm.Body.Replace("##email##", m.Email);
                            mm.IsBodyHtml = true;

                            foreach (PropertyType p in mt.PropertyTypes)
                            {
                                try
                                {
                                    mm.Body = mm.Body.Replace("##" + p.Alias + "##", m.getProperty(p.Alias).Value.ToString());
                                }
                                catch
                                {

                                }
                            }

                            s.Send(mm);

                            m.getProperty("emailSentToAdministrator").Value = 1;
                            m.Save();

                        }

                        Response.Redirect(umbraco.library.NiceUrl(_redirect));

                    }
                    else
                    {

                        lblError.Text = "Error creating user please try a different user name";
                    }
                }

            }
            catch (Exception ex)
            {

                lblError.Text = ex.Message.ToString();
            }
        }
        
        protected void Button1_Click(object sender, EventArgs e)
        {
            try
            {

                if (Page.IsValid)
                {

                    MemberType mt = MemberType.GetByAlias(_memberTypeAlias);
                    Member newMember = Member.MakeNew(((TextBox)ph1.FindControl("txtUserName")).Text, mt, new umbraco.BusinessLogic.User(0));


                    if (!string.IsNullOrEmpty(newMember.LoginName))
                    {
                        Roles.AddUserToRole(newMember.LoginName, _membergroup);

                        newMember.Email = ((TextBox)ph1.FindControl("txtEmail")).Text;
                        newMember.Password = ((TextBox)ph1.FindControl("txtPassword")).Text;
                        newMember.LoginName = ((TextBox)ph1.FindControl("txtUserName")).Text;


                        try
                        {
                            //for surgassist
                            newMember.getProperty("assistantReferralCredits").Value = 0;
                            newMember.getProperty("assistantSMSCredits").Value = 0;
                        }
                        catch
                        {

                        }
                        try
                        {
                            //for surgassist
                            newMember.getProperty("memberApproved").Value = 0;
                        }
                        catch
                        {

                        }

                        newMember.Save();
                        umbraco.library.PublishSingleNode(newMember.Id);


                        //get the properties of the member type
                        Member m = Member.GetMemberFromLoginName(newMember.LoginName);


                        foreach (umbraco.interfaces.IDataType df in _dataFields)
                        {
                            Property p = m.getProperty(df.DataEditor.Editor.ID);
                            df.Data.PropertyId = p.Id;
                            df.DataEditor.Save();

                        }


                        if (_emailadminoncreate == true)
                        {
                            Document doc = new Document(_nodecontainingemail);
                            string emailtext = doc.getProperty(_propertycontainingemail).Value.ToString();

                            MailMessage mm = new MailMessage();
                            SmtpClient s = new SmtpClient();

                            mm.To.Add(_adminemailaddress);
                            mm.Subject = "New " + _memberTypeAlias + " signup " + m.LoginName.ToString();
                            mm.Body = emailtext.ToString();
                            mm.Body = mm.Body.Replace("##loginname##", m.LoginName);
                            mm.Body = mm.Body.Replace("##email##", m.Email);
                            mm.IsBodyHtml = true;

                            foreach (PropertyType p in mt.PropertyTypes)
                            {
                                try
                                {
                                    mm.Body = mm.Body.Replace("##" + p.Alias + "##", m.getProperty(p.Alias).Value.ToString());
                                }
                                catch
                                {

                                }
                            }

                            s.Send(mm);
                      

                            m.getProperty("emailSentToAdministrator").Value = 1;
                            m.Save();

                        }



                        Response.Redirect(umbraco.library.NiceUrl(_redirect));

                    }
                    else
                    {

                        lblError.Text = "Error creating user please try a different user name";
                    }
                }

            }
            catch (Exception ex)
            {

                lblError.Text = ex.Message.ToString();
            }
        }
    }

}